<?php

namespace App\Http\Controllers\Admin;

use App\Admin;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Silber\Bouncer\Bouncer;

class LoginController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        if (!empty(session('admin_info'))) {
            return redirect('admin/index'); //已经登录则直接跳转到首页
        }
        return view('admin/login/index');
    }


    public function create()
    {
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //get param
        $username = $request->get('username');
        $password = $request->get('password');
        //check
        if (empty($username) || empty($password)) {
            return back()->with(['msg'=>'信息填写不全','status'=>0]);
        }
        //find in database
        $admins = Admin::where('username', $username)->first();
        if (empty($admins) || $admins['password'] != md5($password)) {
            return back()->with(['msg'=>'账号或密码错误','status'=>0]);
        }

        //save to session
        session(['admin_info'=>$admins->toArray()]);
        //rediect to index
        return redirect('admin/index');
    }


    public function show($id)
    {
    }


    public function edit($id)
    {
    }


    public function update(Request $request, $id)
    {
    }

    public function destroy($id)
    {
    }

    //退出登录
    public function logout()
    {
        session()->forget('admin_info');
        return ['status'=>1,'msg'=>'退出成功'];
    }
}
